unit CefUtils;

interface

uses ceflib;

type
  TCefRenderProcessHandle = class(TCefRenderProcessHandlerOwn)
  protected
    // procedure OnRenderThreadCreated(const extraInfo: ICefListValue); override;
    // procedure OnWebKitInitialized; override;
    // procedure OnBrowserCreated(const browser: ICefBrowser); override;
    // procedure OnBrowserDestroyed(const browser: ICefBrowser); override;
    // function GetLoadHandler: PCefLoadHandler; override;
    // function OnBeforeNavigation(const browser: ICefBrowser; const frame: ICefFrame;
    // const request: ICefRequest; navigationType: TCefNavigationType;
    // isRedirect: Boolean): Boolean; override;
    // procedure OnContextCreated(const browser: ICefBrowser;
    // const frame: ICefFrame; const context: ICefv8Context); override;
    // procedure OnContextReleased(const browser: ICefBrowser;
    // const frame: ICefFrame; const context: ICefv8Context); override;
    // procedure OnUncaughtException(const browser: ICefBrowser; const frame: ICefFrame;
    // const context: ICefv8Context; const exception: ICefV8Exception;
    // const stackTrace: ICefV8StackTrace); override;
    // procedure OnFocusedNodeChanged(const browser: ICefBrowser;
    // const frame: ICefFrame; const node: ICefDomNode); override;
    function OnProcessMessageReceived(const browser: ICefBrowser;
      sourceProcess: TCefProcessId; const message: ICefProcessMessage)
      : Boolean; override;
  public
    constructor Create; override;
  end;

  TCefBrowserProcessHandle = class(TCefBrowserProcessHandlerOwn)
  protected
    procedure OnContextInitialized; override;
    procedure OnBeforeChildProcessLaunch(const commandLine
      : ICefCommandLine); override;
    procedure OnRenderProcessThreadCreated(const extraInfo
      : ICefListValue); override;
  public
    constructor Create; override;
  end;



  TCefResourceBundleHandle = class(TCefResourceBundleHandlerOwn)
  protected
    function GetDataResource(resourceId: Integer; out data: Pointer;
      out dataSize: NativeUInt): Boolean; override;
    function GetLocalizedString(messageId: Integer; out stringVal: ustring)
      : Boolean; override;
//    function GetDataResourceForScale(resourceId: Integer;
//      scaleFactor: TCefScaleFactor; out data: Pointer;
//      dataSize: NativeUInt): Boolean; override;
  public
    constructor Create; override;
  end;


function InitCef: Boolean;

procedure UnInitCef;

procedure RegisterCustomSchemesCallBack(const registrar: ICefSchemeRegistrar);

procedure BeforeCommandLineProcessingCallBack(const processType: ustring;
  const commandLine: ICefCommandLine);

implementation

  uses Vcl.Dialogs;

function InitCef: Boolean;
begin
  Result := False;
//  CefCache := 'c:\cefbuf';
//  CefLocalesDirPath :=  CefCache;
  CefRemoteDebuggingPort := 9000;

//  CefBrowserProcessHandler := TCefBrowserProcessHandle.Create;
//  CefRenderProcessHandler := TCefRenderProcessHandle.Create;
//  CefOnBeforeCommandLineProcessing := BeforeCommandLineProcessingCallBack;
//  CefOnRegisterCustomSchemes := RegisterCustomSchemesCallBack;

  CefSingleProcess := False;
  //
  if not CefLoadLibDefault then
    Exit;
  //
//  CefRegisterSchemeHandlerFactory('local', '', TCefResourceHandle);

  Result := True;
end;

procedure UnInitCef;
begin
  CefResourceBundleHandler := nil;
  CefBrowserProcessHandler := nil;
  CefRenderProcessHandler := nil;
  CefOnBeforeCommandLineProcessing := nil;
  CefOnRegisterCustomSchemes := nil;
end;

procedure RegisterCustomSchemesCallBack(const registrar: ICefSchemeRegistrar);
begin
 //CefRuntimeTracker.LogMsg('RegisterCustomSchemesCallBack');
end;

procedure BeforeCommandLineProcessingCallBack(const processType: ustring;
  const commandLine: ICefCommandLine);
begin
 //CefRuntimeTracker.LogMsg('BeforeCommandLineProcessingCallBack');
end;

{ TCefRenderProcessHandle }

constructor TCefRenderProcessHandle.Create;
begin
  inherited;
// //CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.Create');
end;

// function TCefRenderProcessHandle.GetLoadHandler: PCefLoadHandler;
// begin
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.GetLoadHandler');
// end;
//
// function TCefRenderProcessHandle.OnBeforeNavigation(const browser: ICefBrowser;
// const frame: ICefFrame; const request: ICefRequest; navigationType: TCefNavigationType;
// isRedirect: Boolean): Boolean;
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnBeforeNavigation');
// end;
//
// procedure TCefRenderProcessHandle.OnBrowserCreated(const browser: ICefBrowser);
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnBrowserCreated');
// end;

// procedure TCefRenderProcessHandle.OnBrowserDestroyed(const browser: ICefBrowser);
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnBrowserDestroyed');
// end;
//
// procedure TCefRenderProcessHandle.OnContextCreated(const browser: ICefBrowser;
// const frame: ICefFrame; const context: ICefv8Context);
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnContextCreated');
// end;
//
// procedure TCefRenderProcessHandle.OnContextReleased(const browser: ICefBrowser;
// const frame: ICefFrame; const context: ICefv8Context);
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnContextReleased');
// end;
//
// procedure TCefRenderProcessHandle.OnFocusedNodeChanged(const browser: ICefBrowser;
// const frame: ICefFrame; const node: ICefDomNode);
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnFocusedNodeChanged');
// end;
//
// function TCefRenderProcessHandle.OnProcessMessageReceived(const browser: ICefBrowser;
// sourceProcess: TCefProcessId; const message: ICefProcessMessage): Boolean;
// begin
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnProcessMessageReceived');
// end;

// procedure TCefRenderProcessHandle.OnRenderThreadCreated(const extraInfo: ICefListValue);
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnRenderThreadCreated');
// end;
//
// procedure TCefRenderProcessHandle.OnUncaughtException(const browser: ICefBrowser;
// const frame: ICefFrame; const context: ICefv8Context; const exception: ICefV8Exception;
// const stackTrace: ICefV8StackTrace);
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnUncaughtException');
// end;
//
// procedure TCefRenderProcessHandle.OnWebKitInitialized;
// begin
// inherited;
////CefRuntimeTracker.LogMsg('TCefRenderProcessHandle.OnWebKitInitialized');
// end;
//
// { TCefBrowserProcessHandle }

constructor TCefBrowserProcessHandle.Create;
begin
  inherited;
 //CefRuntimeTracker.LogMsg('TCefBrowserProcessHandle.Create');
end;

procedure TCefBrowserProcessHandle.OnBeforeChildProcessLaunch(const commandLine
  : ICefCommandLine);
begin
  inherited;
 //CefRuntimeTracker.LogMsg
//    ('TCefBrowserProcessHandle.OnBeforeChildProcessLaunch');
end;

procedure TCefBrowserProcessHandle.OnContextInitialized;
begin
  inherited;
 //CefRuntimeTracker.LogMsg('TCefBrowserProcessHandle.OnContextInitialized');
end;

procedure TCefBrowserProcessHandle.OnRenderProcessThreadCreated(const extraInfo
  : ICefListValue);
begin
  inherited;
 //CefRuntimeTracker.LogMsg
//    ('TCefBrowserProcessHandle.OnRenderProcessThreadCreated');
end;

type
  MyStringMap = class(TCefStringMapOwn);

function TCefRenderProcessHandle.OnProcessMessageReceived
  (const browser: ICefBrowser; sourceProcess: TCefProcessId;
  const message: ICefProcessMessage): Boolean;
begin
  browser.MainFrame.VisitDomProc(
    procedure(const doc: ICefDomDocument)
    var
      p: ICefDomNode;
      stringMap: MyStringMap;
      I: Integer;
    begin
      stringMap := MyStringMap.Create;
      doc.GetElementById('kw').GetElementAttributes(stringMap);
//      for I := 0 to stringMap.GetSize - 1 do
//      begin
//        ShowMessage(stringMap.GetKey(I) + stringMap.GetValue(I));
//      end;
//
//showmessage( doc.GetElementById('J_CheckCode').GetElementAttribute('src'));
//doc.GetElementById('J_CheckCode').

      p := doc.GetElementById('kw');
      if Assigned(p) then
        p.SetElementAttribute('value', '234234');
       //       ProcessNode(doc.Body);
    end);
  Result := True;
end;

{ TCefResourceBundleHandle }

constructor TCefResourceBundleHandle.Create;
begin
  inherited;
 //CefRuntimeTracker.LogMsg('TCefResourceBundleHandle.Create');
end;

function TCefResourceBundleHandle.GetDataResource(resourceId: Integer;
out data: Pointer; out dataSize: NativeUInt): Boolean;
begin
  inherited;
  //CefRuntimeTracker.LogMsg('TCefResourceBundleHandle.GetDataResource');
end;

//function TCefResourceBundleHandle.GetDataResourceForScale(resourceId: Integer; scaleFactor: TCefScaleFactor;
//  out data: Pointer; dataSize: NativeUInt): Boolean;
//begin
//
//end;

function TCefResourceBundleHandle.GetLocalizedString(messageId: Integer;
out stringVal: ustring): Boolean;
begin
  inherited;
 //CefRuntimeTracker.LogMsg('TCefResourceBundleHandle.GetLocalizedString');
end;




initialization
//   CefResourceBundleHandler := TCefResourceBundleHandle.Create;

finalization



end.
